package com.last.service.impl;

import com.api.search.SearchApi;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.last.entity.City;
import com.last.entity.CityDTO;
import com.last.entity.Result;
import com.last.entity.RoomInfo;
import com.last.mapper.CityMapper;
import com.last.service.ICityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class CityServiceImpl extends ServiceImpl<CityMapper, City> implements ICityService {
    @Autowired(required = false)
    private CityMapper cityMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    ObjectMapper objectMapper;

    @Override
    public Result<City> search(City city) {
        Result<City> cityResult = new Result<>();
        if (!cityResult.equals("null")){
            cityResult.setMessage("显示成功");
        }else {
            cityResult.setMessage("显示失败");
        }
        return cityResult;
    }

    @Override
    public List<CityDTO> getCityFromRedis() {

        Set<Object> keys = redisTemplate.boundHashOps("city").keys();
        ArrayList<CityDTO> cityDTO = new ArrayList<>();
        for (Object key : keys) {
            ArrayList areaMap = new ArrayList<>();
            CityDTO dto = new CityDTO();
            dto.setCity(key.toString());
            String city = (String) redisTemplate.boundHashOps("city").get(key);
            try {
                // 包含景点和图片
                List list = objectMapper.readValue(city, List.class);
                HashMap<String, String> hashMap = new HashMap<>();
                for (Object o : list) {
                    String[] split = o.toString().split(",");
                    System.out.println(split.toString());
                    hashMap.put(split[0],split[1]);
                }
                areaMap.add(hashMap);
                dto.setAreaInfo(areaMap);
                cityDTO.add(dto);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }

        }


//        for (Object key : keys) {
//            CityDTO dto = new CityDTO();
//            dto.setCity(key.toString());
//            String areaInfo = (String) redisTemplate.boundHashOps("city").get(key);
//            List list = new ArrayList();
//            try {
//                list = objectMapper.readValue(areaInfo, List.class);
//            } catch (JsonProcessingException e) {
//                e.printStackTrace();
//            }
//            System.out.println(list);
//
//            for (int i = 0; i < list.size(); i++ ) {
//                HashMap<String, String> hashMap = new HashMap<>();
//                String[] split = list.get(i).toString().split(",");
//                for (int j = 0; j < split.length - 1; j++) {
//                    System.out.println(split[j]);
//                    hashMap.put(split[j],split[j+1]);
//                }
//                areaMap.add(hashMap);
//            }

//            dto.setAreaInfo(areaMap);
//            cityDTO.add(dto);
//        }
        return cityDTO;
    }
}
